#https://kvvhost.ru/2019/03/27/led-nightlight-esphome-homeassistant/
esphome:
  name: bedroom_tv_light
  friendly_name: Bedroom_TV_light

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ag1IvfkRMc5NAS/H6xiOZRMLpH8RgizW6N2AOqYtf2E="

ota:
  password: "67ce9c081d9230d4748caa606eba06ee"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bedroom Tvlight Hotspot"
  #  password: "TiTftIj1EKHA"

captive_portal:
    
globals:
  - id: Last_direction
    type: int
    restore_value: no
    initial_value: '1'

light:
  - platform: monochromatic
    name: "Bedroom_TV_Light"
    output: output_light
    id: Bedroom_TV_Light

# Example output entry
output:
  - platform: esp8266_pwm
    id: output_light
    pin: GPIO3
    frequency: 20000 Hz

# # Датчики
# binary_sensor:
#   - platform: gpio
#     pin: GPIO1
#     filters:
#       - invert:
#     name: Bedroom_TV_light_button
#     id: Bedroom_TV_light_button
#     on_click:
#       then:
#       - light.toggle: Bedroom_TV_Light

# # Автоматизация для изменения яркости
#     on_press:
#       then:
#       - if:
#           condition: 
#             light.is_off: Bedroom_TV_Light
#           then:
#           - delay: 0.5s
#           - while:
#               condition:
#                 binary_sensor.is_on: Bedroom_TV_light_button
#               then:
#                 - light.dim_relative:
#                     id: Bedroom_TV_Light
#                     relative_brightness: 5%
#                     transition_length: 0.1s
#                 - delay: 0.1s
#           - lambda: |-
#               id(Last_direction) = 1;
#           else:
#           - delay: 0.5s
#           - lambda: |-
#               if (id(Last_direction) == 1) 
#               id(Last_direction) = -1;
#               else id(Last_direction) = 1;
#           - while:
#               condition:
#                 and:
#                   - binary_sensor.is_on: Bedroom_TV_light_button
#                   - light.is_on: Bedroom_TV_Light
#               then:
#                 - if:
#                     condition:
#                       lambda: |-
#                         return id(Last_direction) == 1;
#                     then:
#                       - light.dim_relative:
#                           id: Bedroom_TV_Light
#                           relative_brightness: 5%
#                           transition_length: 0.1s
#                     else:
#                       - light.dim_relative:
#                           id: Bedroom_TV_Light
#                           relative_brightness: -5%
#                           transition_length: 0.1s
#                 - delay: 0.1s
